package de.baumann.browser.activitys;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.BottomSheetDialog;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.google.zxing.WriterException;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

import de.baumann.browser.R;
import de.baumann.browser.base.BaseOdinActivity;
import de.baumann.browser.base.BasePresenter;
import de.baumann.browser.iview.IBaseView;
import de.baumann.browser.utils.FileUtil;
import de.baumann.browser.utils.QrCodeUtil;
import de.baumann.browser.utils.UserDataKt;
import de.baumann.browser.utils.ViewUtil;

public abstract class AbstractShareActivity<V extends IBaseView, P extends BasePresenter<V>> extends BaseOdinActivity<V, P> implements View.OnClickListener, UMShareListener {

    private BottomSheetDialog sheetDialog;
    private SHARE_MEDIA share_media;
    Bitmap basemap = null;
    UMImage thumb;

    protected void displaySharePanel() {
        sheetDialog = new BottomSheetDialog(mContext);
        sheetDialog.setContentView(R.layout.dialog_share);
        sheetDialog.findViewById(R.id.shareToWechart).setOnClickListener(this);
        sheetDialog.findViewById(R.id.copyLink).setVisibility(View.GONE);
        sheetDialog.findViewById(R.id.shareToQQ).setOnClickListener(this);
        sheetDialog.findViewById(R.id.shareToWXCircle).setOnClickListener(this);
        sheetDialog.findViewById(R.id.shareToSina).setOnClickListener(this);
        sheetDialog.show();
    }

    protected void dimissSharePanel() {
        sheetDialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shareToWechart:
                if (!ViewUtil.isInstalledWX(mContext)) {
                    showShortToast(getString(R.string.not_installed_WeChat));
                    return;
                }
                share_media = SHARE_MEDIA.WEIXIN;
                if (thumb == null) {
                    createShareTemple();
                } else {
                    share(thumb);
                }
                break;
            case R.id.shareToWXCircle:
                if (!ViewUtil.isInstalledWX(mContext)) {
                    showShortToast(getString(R.string.not_installed_WeChat));
                    return;
                }
                share_media = SHARE_MEDIA.WEIXIN_CIRCLE;
                if (thumb == null) {
                    createShareTemple();
                } else {
                    share(thumb);
                }
                break;
            case R.id.shareToQQ:
                if (!ViewUtil.isInstalledQQ(mContext)) {
                    showShortToast(getString(R.string.not_installed_QQ));
                    return;
                }
                share_media = SHARE_MEDIA.QQ;
                if (thumb == null) {
                    createShareTemple();
                } else {
                    share(thumb);
                }
                break;
            default:
        }
    }

    private void createShareTemple() {
        showLoading();
        handler.post(runnable);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 3000) {
                hideLoading();
                handler.removeCallbacks(runnable);
                UMImage umImage = new UMImage(AbstractShareActivity.this, basemap);
                thumb = new UMImage(AbstractShareActivity.this, basemap);
                umImage.setThumb(thumb);
                share(umImage);
            }
        }
    };

    Runnable runnable = () -> {
        basemap = FileUtil.getImageFromAssetsFile(mContext, "image_share_temple.png").copy(Bitmap.Config.ARGB_8888, true);
        double scale = (double) basemap.getWidth() / 375;
        BigDecimal bd = new BigDecimal(scale);
        scale = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        int size = (int) (150 * scale);
        Bitmap codeBitmap = QrCodeUtil.createQRCodeWithLogo(UserDataKt.getShareContent(), size, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        basemap = QrCodeUtil.compositeShareImage(basemap, codeBitmap, new int[]{(int) (220 * scale), (int) (341 * scale)});
        handler.sendEmptyMessage(3000);
    };


    private void share(UMImage umImage) {
        new ShareAction(AbstractShareActivity.this)
                .setPlatform(share_media)
                .withMedia(umImage)
                .setCallback(this)
                .share();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {
    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
    }

    @Override
    public boolean showBindEth() {
        return false;
    }

}
